﻿Imports System.Text
Imports Microsoft.LightSwitch.Threading
Imports System.Windows.Browser

Namespace LightSwitchApplication

    Public Class SearchMissingNeuter

        Private Sub SearchMissingNeuter_Created()
            Dim CSVButton = Me.FindControl("ExportCSV")
            AddHandler CSVButton.ControlAvailable, AddressOf ExportCSV_ControlAvailable

            'For Each control In Me.FindControls()
            'If Control.DisplayName = "Export To CSV" Then
            'AddHandler Control.ControlAvailable, AddressOf ExportCSV_ControlAvailable
            'End If
            'Next
        End Sub

        Private Sub ExportCSV_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)
            RemoveHandler Me.FindControl("ExportCSV").ControlAvailable, AddressOf ExportCSV_ControlAvailable
            Dim Button = DirectCast(e.Control, Button)
            AddHandler Button.Click, AddressOf ExportCSV_Click

            'On Error Resume Next
            'AddHandler CType(e.Control, System.Windows.Controls.Button).GotFocus, AddressOf ExportCSV_Click

        End Sub

        Private Sub ExportCSV_Click(sender As Object, e As System.Windows.RoutedEventArgs)
            Dim collectionProperty As Microsoft.LightSwitch.Details.Client.IScreenCollectionProperty = Me.Details.Properties.MissingNeuter
            Dim intPageSize = collectionProperty.PageSize
            'Get the Current PageSize and store to variable
            collectionProperty.PageSize = 0

            Dim dialog = New SaveFileDialog()
            'dialog.DefaultFileName = collectionProperty.DisplayName
            dialog.Filter = "Comma Separated Values (*.csv)|*.csv"
            If dialog.ShowDialog() = True Then

                Using stream As New StreamWriter(dialog.OpenFile())
                    Dim csv As String = GetCSV()
                    stream.Write(csv)
                    stream.Close()
                    Me.ShowMessageBox("CSV File Created Successfully. ", "CSV Export", MessageBoxOption.Ok)
                End Using
            End If
            collectionProperty.PageSize = intPageSize

            'Reset the Current PageSize
        End Sub

        '//Selected columns
        'Private Function GetCSV() As String
        '    Dim csv As New StringBuilder()
        '    csv.AppendFormat("Heading1 , Heading2, Heading3" & "," & System.Environment.NewLine)
        '    For Each c In Me.EvacuationRegistrations
        '        csv.AppendFormat(Convert.ToString(c.first_name) & "," & Convert.ToString(c.last_name) & "," & Convert.ToString(c.location) & "," & System.Environment.NewLine, c)
        '    Next
        '    Return csv.ToString(0, (csv.Length - 1))
        'End Function

        Private Function GetCSV() As String
            Dim csv As New StringBuilder()

            Dim i As Integer = 0

            For Each orderRow In MissingNeuter
                '//HEADER
                If i = 0 Then
                    Dim c As Integer = 0
                    For Each prop In orderRow.Details.Properties.All().OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()
                        If c > 0 Then
                            'csv.Append(vbTab)
                            csv.Append(",")
                        End If
                        c = c + 1
                        csv.Append(prop.DisplayName)
                    Next
                End If
                csv.AppendLine("")

                '//DATA ROWS

                Dim c1 As Integer = 0
                For Each prop In orderRow.Details.Properties.All().OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()
                    If c1 > 0 Then
                        'csv.Append(vbTab)
                        csv.Append(",")
                    End If
                    c1 = c1 + 1
                    csv.Append(prop.Value)
                Next
                i = i + 1
            Next

            If csv.Length > 0 Then
                Return csv.ToString(0, csv.Length - 1)
            Else
                Return ""
            End If
        End Function

        '//Selected columns
        Private Function GetCSV_old() As String
            Dim csv As New StringBuilder()
            Dim i As Integer = 0

            For Each a In MissingNeuter
                If i = 0 Then
                    csv.AppendFormat("OrderId" & "," & "OrderDate" & System.Environment.NewLine, a)
                End If
                csv.AppendFormat(a.Name & "," & a.AgreeToSpayNeuterByDate & System.Environment.NewLine, a)
                i = i + 1
            Next

            If csv.Length > 0 Then
                Return csv.ToString(0, csv.Length - 1)
            Else
                Return ""
            End If
        End Function

    End Class
End Namespace
